package cn.com.ddapp.ddm.ui.mine;

import android.view.Gravity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import butterknife.BindView;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.mine.InfoDetails;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;


public class InfoDetailsActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.msg_webview)
    WebView wv_news_detail_content;
    private TextView tvTitle, tvContent, tvTime;
    private String userId;
    int id;

    OkUtilCallback callback;

    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        //获取数据
        getData();
    }*/

    @Override
    public void initView() {

        titletext.setText("消息详情");
        tvTitle = (TextView) findViewById(R.id.tv_details_title);
        tvContent = (TextView) findViewById(R.id.tv_details_content);
        tvTime = (TextView) findViewById(R.id.tv_details_time);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        WebSettings settings = wv_news_detail_content.getSettings();
        // 设置自适应屏幕，两者合用
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
//            settings.setTextSize(WebSettings.TextSize.LARGER);//设置字体大小
        settings.setDefaultFontSize(40);
        settings.setJavaScriptEnabled(true);
//        wv_news_detail_content.setInitialScale(100);
        // 设置可以缩放
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);

        settings.setDisplayZoomControls(false);
        settings.setDisplayZoomControls(false);

        wv_news_detail_content.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                mEmptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                super.onPageFinished(view, url);
            }

        });
    }


    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        id = getIntent().getIntExtra("id",0);
//        Log.e("11111",id);
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            callback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    InfoDetails details = gson.fromJson(response, InfoDetails.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(details.getResult()))) {
                        mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(details.getResult()))) {
                        BaseApplication.getIntstance().showToast(details.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else {
                        if (details.getMessage() != null) {

                            tvTime.setText(details.getCreateTimeStr());//设置时间
                            tvTitle.setText(details.getMessage().getTitle());//设置标题
                            tvContent.setText(details.getMessage().getContent());//设置内容
                            wv_news_detail_content.loadData(details.getMessage().getContent(), "text/html;charset=UTF-8", null);
                        }
                    }
                }
            };
        }
        //请求消息详情
        OKUtilAPI.post(OKUtilAPI.mdetail, "userId=" + userId+"&id="+id, callback);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_info_details;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    /**
     * 获取数据
     */
    /*private void getData() {
        RequestQueue mQueue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(Request.Method.POST, "http://139.224.69.131:8080//app/user/mdetail.api",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject j1 = new JSONObject(response);
                            if ("OK".equals(j1.getString("result"))) {
                                tvTime.setText(j1.getString("createTimeStr"));//设置时间
                                JSONObject j2 = j1.getJSONObject("message");
                                tvTitle.setText(j2.getString("title"));//设置标题
                                tvContent.setText(j2.getString("content"));//设置内容
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("userId", userId);
                params.put("id", id);
                return params;
            }
        };
        mQueue.add(request);
    }*/
}
